package com.example.cici.cclight.fragment;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import com.example.cici.cclight.OnColorChangeListener;
import com.example.cici.cclight.R;
import com.example.cici.cclight.util.SocketUtil;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.UUID;

public class BlockChangeFragment extends Fragment{
    private SeekBar seekBar_R;
    private SeekBar seekBar_G;
    private UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//与外置蓝牙相适配的代码
    private ArrayList<BluetoothDevice> strArr;
    private SeekBar seekBar_B;
    private OnColorChangeListener onColorChangeListener;
    private int Rr;
    private int G;
    private int B;
    private Button button;
    private BluetoothSocket socket;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_blockchange, null);
        button = (Button) view.findViewById(R.id.comfirm_color);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendLight();
            }
        });
//        final CustomViews customViews = new CustomViews(this.getContext());
        seekBar_R = (SeekBar) view.findViewById(R.id.r);
        seekBar_R.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Rr = Integer.parseInt(String.valueOf(seekBar.getProgress()));
//                customViews.setBackgroundColor(Color.rgb(Rr,G,B));
                onColorChangeListener.onChange(Rr,G,B);
                Log.e("onChangeRr",Rr+"");

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar_G = (SeekBar) view.findViewById(R.id.g);
        seekBar_G.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                G = Integer.parseInt(String.valueOf(seekBar.getProgress()));
//                customViews.setBackgroundColor(Color.rgb(Rr,G,B));
                onColorChangeListener.onChange(Rr,G,B);
                Log.e("onChangeG",G+"");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seekBar_B = (SeekBar) view.findViewById(R.id.b);
        seekBar_B.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                B = Integer.parseInt(String.valueOf(seekBar.getProgress()));
//                customViews.setBackgroundColor(Color.rgb(Rr,G,B));
                onColorChangeListener.onChange(Rr,G,B);
                Log.e("onChangeB",B+"");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        return view;
    }
    public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener){
        this.onColorChangeListener = onColorChangeListener;
    }
    private void sendLight() {
        OutputStream os = null;
        try {
            socket=SocketUtil.getInstance().getBluetoothSocket();
            os = socket.getOutputStream();
            os.write(("+C,12:"+Rr+","+G+","+B+";").getBytes());
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void sendMessage(final int i) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OutputStream os = null;
                try {
                    socket = strArr.get(i).createRfcommSocketToServiceRecord(uuid);
                    socket.connect();
                    os = socket.getOutputStream();
                    os.write("+C,12:255,0,0;".getBytes());
                    os.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
